package org.zk.controller;

import com.google.gson.Gson;
import org.zk.entity.Catelog;
import org.zk.service.CatelogService;
import org.zk.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

//  catelog/addCatelog
@Controller
@RequestMapping(value = "/catelog")
public class CatelogController {

    @Autowired
    CatelogService catelogService;

    Gson gson = new Gson();

    /**
     * 添加分类请求
     * @return
     */
    @RequestMapping(value = "/addCatelog")
    public ModelAndView addCatelog(){

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/test/catelogtest/insertCatelog.jsp");
        return mv;
    }

    /**
     * 添加分类表单处理  返回值String就是转发与重定向 ModelAndView返回页面
     * @param request
     * @param catelog
     * @param file
     * @return
     */
//    @RequestMapping(value = "/addCatelogPost", method = RequestMethod.POST)
//    public String addCatelogPost(HttpServletRequest request, Catelog catelog, @RequestParam(value = "imgFile") MultipartFile file){
//
//
//        ModelAndView mv = new ModelAndView();
//        catelog.setStatus((byte)1);
////        项目根目录的绝对路径
//        String webAppPath = request.getServletContext().getRealPath("/index").replace("\\","/index");
//        String path = webAppPath + "upload/catelog/";
////        System.out.println("path:"+ path);
//        File pathDir = new File(path);
//        if(! pathDir.exists()){
//            pathDir.mkdir();
//        }
////        获取文件名
//        String fileName = file.getOriginalFilename();
////        取得后缀名
//        String extName = fileName.substring(fileName.lastIndexOf(".")+1);
////        取别名 防止同名
//        String newFileName = String.valueOf(System.currentTimeMillis()) + "." + extName;
//
//        File saveFile = new File(path, newFileName);
//
//        try {
//            //核心 将multipart拷贝到文件中
//            file.transferTo(saveFile);
//
//            catelog.setImg("/upload/catelog/" + saveFile.getName());
//            catelogService.insertCatelog(catelog);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "catelogList";
//
//    }

    /**
     * 所有分类
     * 接口1：加载目录
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getCateLog", method = {RequestMethod.GET,RequestMethod.POST}, produces = "text/plain;charset=utf-8")
    public String getCateLog(){

        List<Catelog> catelogList = catelogService.getCateLog();
//        System.out.println(catelogList.get(1).getName());

        Msg msg = new Msg(0,"");
        if(catelogList != null){
            msg.setCode(1);
            msg.setData(catelogList);
        }
        return gson.toJson(msg);
    }

    /**
     * 删除一个分类   参数解析也是按照同名赋值的而不是顺序
     */
//    @RequestMapping(value = "/removeCatelog")
//    public String removeCatelog(Integer id){
//        System.out.println(id);
//        if(catelogMapper.deleteCatelogById(id) >0){
//            return "catelogList";
//        }else{
//            return "catelogList";
//        }
//    }



}
